import os
import time
import re
import getopt
import sys

options = getopt.getopt(sys.argv[1:], "y:v:a:s:d:", ["year=", "volume=", "article=", "help", "src-path=", "dst-path"])
year = reduce(lambda v,x:((x[0]=="-y" or x[0]=="--year") and x[1]) or v, options[0], "")
volume = reduce(lambda v,x:((x[0]=="-v" or x[0]=="--volume") and x[1]) or v, options[0], "")
article = reduce(lambda v,x:((x[0]=="-a" or x[0]=="--article") and x[1]) or v, options[0], "")
article_path = reduce(lambda v,x:((x[0]=="-s" or x[0]=="--src-path") and x[1]) or v, options[0], "./")
dst_path = reduce(lambda v,x:((x[0]=="-d" or x[0]=="--dst-path") and x[1]) or v, options[0], "./")

def process_article(year, volume, article, dst_path):
   article_file = article_path + "Le\\ Scienze_" + year + "_" + volume + "_" + article + ".pdf.swf"
   lines = os.popen("swfextract " + article_file).read()
   jpgs = re.findall("JPEGs: ID\(s\) (.*)", lines)[0].split(", ")
   fd = open(dst_path + "out.html", "w")
   fd.write("<html><body>")
   for (jpg, i) in [(jpgs[i], i+1) for i in range(len(jpgs))]:
      if (i%30 == 1):
         print "Volume " + volume + " Article " + article + " row " + str(i) + "/" + str(len(jpgs))
      os.popen("swfextract -j " + jpg + " -o " + dst_path + jpg + ".jpg " + article_file).read()
      fd.write("<img border=0 src=\"./"+jpg+".jpg\"/>")
   
   fd.write("</body></html>")
   fd.close()

def process_volume(year,volume, dst_path):
   files = os.listdir(article_path)
   match = "Le Scienze_" + year + "_" + volume + "_(.*?)\."
   articles = [re.findall(match, file_name)[0] for file_name in os.listdir(article_path) if re.findall(match, file_name)]
   fd = open(dst_path + "out.html", "w")
   fd.write("<html><body>")

   for (article, i) in [(articles[i], i+1) for i in range(len(articles))]:
      print "Volume " + volume + " Article " + str(i) + "/" + str(len(articles))
      dst_article_path = dst_path + article + "/"
      # va fatto il try
      os.mkdir(dst_article_path)
      process_article(year, volume, article, dst_article_path)
      fd.write("<a target=\"article_frame\" href=\"" + article + "/out.html\">["+article+"]</a>")

   fd.write("<br><iframe width=\"100%\" height=\"90%\" name=\"article_frame\"></iframe></body></html>")
   fd.close()
   
def process_year(year, dst_path):
   files = os.listdir(article_path)
   match = "Le Scienze_" + year + "_(.*?)_"
   volumes = [x for x in set([re.findall(match, file_name)[0] for file_name in os.listdir(article_path) if re.findall(match, file_name)])]

   fd = open(dst_path + "out.html", "w")
   fd.write("<html><body>")

   for (volume, i) in [(volumes[i], i+1) for i in range(len(volumes))]:
      print "Volume " + str(i) + "/" + str(len(volumes))
      dst_volume_path = dst_path + volume + "/"
      # va fatto il try
      os.mkdir(dst_volume_path)
      process_volume(year, volume, dst_volume_path)
      fd.write("<a target=\"volume_frame\" href=\"" + volume + "/out.html\">["+volume+"]</a>")

   fd.write("<br><iframe width=\"100%\" height=\"95%\" name=\"volume_frame\"></iframe></body></html>")
   fd.close()

start_time = time.time()

if (year != "" and volume != "" and article != ""):
   process_article(year, volume, article, dst_path)
elif (year != "" and volume != ""):
   process_volume(year,volume, dst_path)
elif (year != ""):
   process_year(year,dst_path)
else:
   print """
         Usage "year=", "volume=", "article=", "help", "src-path=", "dst-path"
         """

total_time = time.time() - start_time
print "Processing time: " + str(int(total_time)/3600) + "h " + str((int(total_time)%3600)/60) + "m " + str(int(total_time)%60) + "s"

# puo' un processo coprire due ruoli differenti?
